Online PHP Function(s){ #Test PHP functions online!; }

  •  
 

 
Added: 08/01/2012 by John

Simple Memcache implementation

(Custom class by John)

Description

You can use this class to simply cache your data with Memcache in PHP.

Requirements

- PHP 5.3 or higher (you can change "__construct" to "cache" to make it work in older versions)
- Memcache extention installed (how to install the Memcached extention?)
- Memcached installed on a server (how to install Memcached?)


cache.php

Save the code below to a file named cache.php, and include this file in your php code (include "cache.php";)

<?php
     
# Copyright 2012 John Post - StarfixIT
     # NOTE: Requires PHP version 5.3 or later with Memcache module
    
class cache
        
{
        
            private 
$host '127.0.0.1';
            private 
$port '11211';
            private 
$memcache;
            private 
$lifetime;
            private 
$name;

            function 
__construct($name false$lifetime 600)
            {
              
//Check if Memcache is installed
                
if(!class_exists("Memcache")){
                    exit(
"You need to install memcache");
                } 
                
//Check if allready connected to memcached
                
if (isset($_GLOBALS["memcache"])) {
                    
//Yes, use old connection
                    
$this->memcache $_GLOBALS["memcache"];
                } else {
                    
//No, make new connection
                    
$this->memcache = new Memcache;
                    
$this->memcache->connect($this->host$this->port);
                    
$_GLOBALS["memcache"] = $this->memcache;
                }
                
//Set global livetime
                
$this->lifetime $lifetime;
                
                
//Set global name
                
if ($name !== false) {
                    
$this->name $this->makeNameOk($name);
                }
            }
            
            
//Public function to set the name
            
public function setName($name)
            {
                
$this->name $this->makeNameOk($name);
            }
            
            
//Public function to set the cache, livetime optional
            
public function setCache($data$lifetime false)
            {
                
$lifetime $lifetime === false $this->lifetime $this->lifetime;
                
$this->memcache->set($this->name$dataMEMCACHE_COMPRESSED$lifetime);
            }
            
            
//Public function to get the cache, optional name
            
public function getCache($name false)
            {
                
$name $name === false $this->name $name;
                
$result $this->memcache->get($name);
                if (
$result !== false) {
                    return 
$result;
                }
                return 
false;
            }
            
            
//Public function to remove the data from Memcached
            
public function remove$name false )
            {
                
$name $name === false $this->name $name;
                
$this->memcache->delete$name );
            }
            
            
//Function to create a clean alias
            
private function makeNameOk($name)
            {
                return 
preg_replace('/[^A-Za-z0-9_]/'""$name);
            }

        }


Example

<?php
//Include Cache class
include "cache.php";

//Open the class with name "myData" and 900 secs lifetime
$cache = new cache("myData"900);

//Get the data from cache
$data $cache->getCache();

if( 
$data === false ){
    
//if the cache is expired, run the expensive function
    
$data very_expensive_function();
    
    
//Save the data to cache
    
$cache->setCache$data );
    echo 
"Cache set:<br/>";
}else{
    echo 
"Got data from cache:<br/>";
}

//Output the data
echo $data;

//This is just a silly example of an expensive function (takes about 2 seconds)
function very_expensive_function(){
    
$count 19500000;
    
$time_start microtime(true);
    for(
$i 0$i $count; ++$i);
        
$i 0; while($i $count) ++$i;
    return 
number_format(microtime(true) - $time_start3);
}



 

 

 


Comments

 
 
      © 2019 OnlinePHPFunctions.com | Disclaimer |       PHP versions: 7.2.4, 7.1.0, 7.0.14, 7.0.5, 7.0.4, 7.0.3, 7.0.2, 7.0.1, 5.6.29, 5.6.20, 5.6.19, 5.6.18, 5.6.17, 5.6.2, 5.5.34, 5.5.33, 5.5.32, 5.5.31, 5.5.18, 5.5.5, 5.5.0.a6, 5.5.0.a.5, 5.5.0.a.2, 5.4.34, 5.4.21, 5.4.13, 5.4.12, 5.4.11, 5.4.10, 5.4.9, 5.4.8, 5.4.7, 5.4.6, 5.4.5, 5.4.4, 5.4.3, 5.4.2, 5.4.1, 5.4.0, 5.3.29, 5.3.27, 5.3.23, 5.3.22, 5.3.21, 5.3.20, 5.3.19, 5.3.18, 5.3.17, 5.3.16, 5.3.15, 5.3.14, 5.3.13, 5.3.12, 5.3.11, 5.3.10, 5.3.2, 5.3.1, 5.3.0, 5.2.17, 5.2.16, 5.1.6, 5.1.5, 5.0.5, 5.0.4, 4.4.9